import { PrismaClient } from "@prisma/client";
import * as bcrypt from "bcrypt";

const prisma = new PrismaClient();

async function main() {
  console.log("开始初始化数据库...");

  // 1. 创建三种角色的用户
  const adminPassword = await bcrypt.hash("admin123", 10);
  const editorPassword = await bcrypt.hash("editor123", 10);
  const userPassword = await bcrypt.hash("user123", 10);

  const admin = await prisma.user.upsert({
    where: { username: "admin" },
    update: {},
    create: {
      username: "admin",
      password: adminPassword,
      email: "admin@aiproducthome.com",
      role: "admin",
      status: true,
      mobile: "13800000001",
    },
  });
  const editor = await prisma.user.upsert({
    where: { username: "editor" },
    update: {},
    create: {
      username: "editor",
      password: editorPassword,
      email: "editor@aiproducthome.com",
      role: "editor",
      status: true,
      mobile: "13800000002",
    },
  });
  const normalUser = await prisma.user.upsert({
    where: { username: "user001" },
    update: {},
    create: {
      username: "user001",
      password: userPassword,
      email: "user001@aiproducthome.com",
      role: "user",
      status: true,
      mobile: "13800000003",
    },
  });
  console.log("用户创建成功:", [admin.username, editor.username, normalUser.username]);

  // 2. 创建文章分类
  const categories = await Promise.all([
    prisma.articleCategory.upsert({
      where: { name: "技术趋势" },
      update: {},
      create: {
        name: "技术趋势",
        description: "AI技术发展趋势和前沿动态",
        sort: 1,
        status: true,
      },
    }),
    prisma.articleCategory.upsert({
      where: { name: "实用工具" },
      update: {},
      create: {
        name: "实用工具",
        description: "实用的AI工具和软件推荐",
        sort: 2,
        status: true,
      },
    }),
    prisma.articleCategory.upsert({
      where: { name: "产品动态" },
      update: {},
      create: {
        name: "产品动态",
        description: "AI产品发布和更新信息",
        sort: 3,
        status: true,
      },
    }),
    prisma.articleCategory.upsert({
      where: { name: "行业分析" },
      update: {},
      create: {
        name: "行业分析",
        description: "AI行业深度分析和洞察",
        sort: 4,
        status: true,
      },
    }),
    prisma.articleCategory.upsert({
      where: { name: "教程指南" },
      update: {},
      create: {
        name: "教程指南",
        description: "AI使用教程和操作指南",
        sort: 5,
        status: true,
      },
    }),
  ]);
  console.log(
    "文章分类创建成功:",
    categories.map(c => c.name)
  );

  // 3. 创建示例文章
  const articles = await Promise.all([
    prisma.article.upsert({
      where: { id: 1 },
      update: {},
      create: {
        title: "深度学习的未来：机遇与挑战",
        slug: "dl-future-20250109",
        content: "<h2>引言</h2><p>从 GPT-4 到 Sora，我们正处于一个技术爆炸的时代。人工智能的发展速度远超想象，为各个行业带来了前所未有的机遇。</p>",
        summary: "从 GPT-4 到 Sora，我们正处于一个技术爆炸的时代，但前路并非一帆风顺...",
        author: "admin",
        category: {
          connect: { id: categories[0].id },
        },
        status: true,
        tags: ["深度学习", "AI技术", "行业趋势"],
        views: 2100,
        likes: 113,
        comments: 13,
        isTop: true,
        isRecommend: true,
        publishedAt: new Date("2025-01-09"),
        user: {
          connect: { id: admin.id },
        },
      },
    }),
    prisma.article.upsert({
      where: { id: 2 },
      update: {},
      create: {
        title: "普通人如何利用 AI 提升工作效率？",
        slug: "ai-for-everyone-20250108",
        content: "<h2>前言</h2><p>本文介绍5个实用 AI 工具，无需编程背景，普通人也能轻松上手。</p>",
        summary: "本文介绍5个实用 AI 工具，无需编程背景...",
        author: "editor",
        category: {
          connect: { id: categories[1].id },
        },
        status: true,
        tags: ["AI工具", "工作效率", "实用指南"],
        views: 5800,
        likes: 204,
        comments: 45,
        isRecommend: true,
        publishedAt: new Date("2025-01-08"),
        user: {
          connect: { id: editor.id },
        },
      },
    }),
    prisma.article.upsert({
      where: { id: 3 },
      update: {},
      create: {
        title: "Runway发布Gen-3，视频生成效果再逼真",
        slug: "runway-gen3-20250722",
        content: "<h2>产品介绍</h2><p>Gen-3 模型在人物一致性、动作流畅度方面有了显著提升。</p>",
        summary: "Gen-3 模型在人物一致性、动作流畅度...",
        author: "user001",
        category: {
          connect: { id: categories[2].id },
        },
        status: false,
        tags: ["Runway", "视频生成", "AI产品"],
        views: 9100,
        likes: 340,
        comments: 89,
        publishedAt: null,
        user: {
          connect: { id: normalUser.id },
        },
      },
    }),
  ]);
  console.log(
    "示例文章创建成功:",
    articles.map(a => a.title)
  );

  console.log("数据库初始化完成！");
}

main()
  .catch(e => {
    console.error("数据库初始化失败:", e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
